/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TStockAlarm;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TStockAlarmDao;
import com.ruipengkj.service.TStockAlarmService;
/**
 * 说明：
 * 库存报警Service层接口
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TStockAlarmServiceImpl implements TStockAlarmService{
	@Resource
	private TStockAlarmDao TStockAlarmDao;//DAO层
	//查询所有
	@Override
	public List<TStockAlarm> findService(TStockAlarm entity) throws Exception {
		String hql = "select o from TStockAlarm o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getStoreID()!=null){
				whereHQL.append("StoreID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getStoreID()+"%");
			}
			if(entity.getTCommodity()!=null&&!entity.getTCommodity().getCommodityID().equals("")){
				whereHQL.append("TCommodity=?").append(BaseQuery.AND);
				param.add(entity.getTCommodity());
			}
			if(entity.getType()!=null&&!entity.getType().equals("")){
				whereHQL.append("type=?").append(BaseQuery.AND);
				param.add(entity.getType());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockAlarmDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//根据条件翻页查询
	@Override
	public PageModel<TStockAlarm> getScrollService(int firstResult,
			int maxResult, TStockAlarm entity) throws Exception {
		String hql = "select o from TStockAlarm o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getStoreID()!=null&&!entity.getStoreID().equals("")){
				whereHQL.append("o.storeID=?").append(BaseQuery.AND);
				param.add(entity.getStoreID());
			}
			if(entity.getTCommodity()!=null&&!entity.getTCommodity().getCommodityID().equals("")){
				whereHQL.append("o.TCommodity=?").append(BaseQuery.AND);
				param.add(entity.getTCommodity());
			}
			if(entity.getOperator()!=null&&!entity.getOperator().equals("")){
				whereHQL.append("o.operator").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getOperator()+"%");
			}
			//按时间查询
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("o.happenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("o.happenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockAlarmDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	//修改
	@Override
	public void modifyService(TStockAlarm entity) throws Exception {
		TStockAlarmDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TStockAlarm entity) throws Exception {
		TStockAlarmDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TStockAlarmDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TStockAlarm getByIdService(String id) throws Exception {
		return TStockAlarmDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TStockAlarm entity) throws Exception {
		TStockAlarmDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TStockAlarm entity) throws Exception {
		return TStockAlarmDao.saveRetDao(entity);
	}
	public TStockAlarmDao getTStockAlarmDao() {
		return TStockAlarmDao;
	}

	public void setTStockAlarmDao(TStockAlarmDao TStockAlarmDao) {
		this.TStockAlarmDao = TStockAlarmDao;
	}
	
	@Override
	public List<TStockAlarm> findExportService(
			TStockAlarm entity) throws Exception {
		String hql = "select o from TStockAlarm o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getStoreID()!=null&&!entity.getStoreID().equals("")){
				whereHQL.append("StoreID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getStoreID()+"%");
			}
			if (entity.getHappenTime() != null
					&& !entity.getHappenTime().equals("")) {
				whereHQL.append("o.happenTime >=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if (entity.getEndTime() != null && !entity.getEndTime().equals("")) {
				whereHQL.append("o.happenTime <=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockAlarmDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
}
